package com.dreem.principle.ocp;

/**
 * @program: DesignParrtern
 * @description: 开闭原则
 * @author: Peng
 * @create: 2021-01-16  20:49
 **/
public class Ocp {
    public static void main(String[] args){
        new GraphicEditor().drawShape(new Rectangle());
    }
}

// 这是一个用于绘图的类
class GraphicEditor{
    // 接收Shape对象，然后根据type，来绘制不同的图形
    public void drawShape(Shape shape){
        if(shape.m_type == 1){
            drawRectangle(shape);
        }else if(shape.m_type == 2){
            drawCircle(shape);
        }
    }

    private void drawCircle(Shape shape) {
        System.out.println("圆形");
    }

    private void drawRectangle(Shape shape) {
        System.out.println("矩形");
    }
}

// Shape类，基类
class Shape{
    int m_type;
}

class Rectangle extends Shape {
    public Rectangle(){
        super.m_type = 1;
    }
}

class Circle extends Shape {
    Circle(){
        super.m_type = 2;
    }
}